import java.io.Serializable;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

class UserBean implements Serializable {
    private static final long serialVersionUID = 1L;
    private String sqlurl;
    private String sqlname;
    private String sqlpasswd;
    private String username ;
    private String passwd;
    private String familyname;
    private String name ;
    private String email;

    public UserBean( String sqlurl, String sqlname, String sqlpasswd,String username , String passwd,String familyname,String name, String email) {
        this.username = username;
        this.passwd = passwd;
        this.sqlurl = sqlurl;
        this.sqlname = sqlname;
        this.sqlpasswd = sqlpasswd;
        this.name = name;
        this.email = email;
        this.familyname = familyname;

    }
    public UserBean( String sqlurl, String sqlname, String sqlpasswd,String username , String passwd) {
        this.username = username;
        this.passwd = passwd;
        this.sqlurl = sqlurl;
        this.sqlname = sqlname;
        this.sqlpasswd = sqlpasswd;
    }

    public void setName(String name) {
        this.name = name;
    } public String getName() {
        return name;
    }
    public void setFamilyname(String familyname) {
        this.familyname = familyname;
    }
    public String getFamilyname() {
        return familyname;
    };

    public void setEmail(String email) {
        this.email = email;
    }
    public String getEmail() {
        return email;
    };

    public void setUsername(String username) {
        this.username = username;
    }
    public void setPasswd(String passwd) {
        this.passwd = passwd;
    }
    public void setSqlurl(String sqlurl) {
        this.sqlurl = sqlurl;
    }
    public void setSqlname(String sqlname) {
        this.sqlname = sqlname;
    }
    public void setSqlpasswd(String sqlpasswd) {
        this.sqlpasswd = sqlpasswd;
    }
    public String getUsername() {
        return username;
    }
    public String getPasswd() {
        return passwd;
    }
    public String getSqlurl() {
        return sqlurl;
    }
    public String getSqlname() {
        return sqlname;
    }
    public String getSqlpasswd() {
        return sqlpasswd;
    }

    public boolean checkpass() {
        try (Connection conn = DriverManager.getConnection(sqlurl, sqlname, sqlpasswd)) {
            Statement stmt = conn.createStatement(java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE, java.sql.ResultSet.CONCUR_READ_ONLY);
	    System.out.println("saf");

            ResultSet rs = stmt.executeQuery("select * from user where username='" + username + "'");

            String sqluserpasswd = null;
            if(rs.next())
                sqluserpasswd = rs.getString("passwd");
            if(sqluserpasswd == null || sqluserpasswd.equals(passwd) == false) {
                return false;
	    } else {
		return true;
	    }
        } catch (SQLException e) {
            e.printStackTrace();
        }

	return false;

    }
    public ResultSet selectuser() {
        try (Connection conn = DriverManager.getConnection(sqlurl, sqlname, sqlpasswd)) {
            Statement stmt = conn.createStatement(java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE, java.sql.ResultSet.CONCUR_READ_ONLY);

            ResultSet rs = stmt.executeQuery("select * from user where username='" + username + "'");

            if(rs.next())
	    {
		System.out.println(rs);
		return rs;

	    }
	    else return null;
        } catch (SQLException e) {
            e.printStackTrace();
        }

	return null;

    }

    @Deprecated
    public void updateSql() {

        try (Connection conn = DriverManager.getConnection(sqlurl, sqlname, sqlpasswd)) {
            Statement stat = conn.createStatement();

            stat.execute(" insert into `user` (`username`,`passwd`,`familyname`,`name`,`email`) values ('" + username + "','" + passwd + "', '" + name.substring(0, 1) + "','" + name.substring(1, name.length()) + "','" + email + "' );");

        } catch (SQLException e) {
            e.printStackTrace();
        }


    }

}
